import { defineStore } from "pinia";
import { ref } from "vue";
import { loginApi } from "@/apis/user";
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import { useRouter } from "vue-router";


const router = useRouter()

export const useUserStore = defineStore('user', () => {
    const userInfo = ref({})
    const token = ref('')

    //登录成功保存用户信息
    const getUserInfo = async (account, password) => {
        const res = await loginApi({ account, password });
        if (res.code == 1) {
            console.log(res.result);
            userInfo.value = res.result;
            token.value = res.result.token;
            return { success: true, data: res.data };
        } else {
            return { success: false, message: res.message || '登录失败' };
        }
    };

    const clearUserInfo = () => {
        userInfo.value = {};
    }


    return {
        userInfo,
        token,
        getUserInfo,
        clearUserInfo,
    }
},
{
    persist: true,
})